<?php

class QrcodeModel extends Model
{
    
    private $accountsConf = array(
        'yunxinchuanying' => array(
            'appid' => 'wxde40c1f7be9ce9a7',
            'appsecret' => 'e20267e6a6ebf4fb61842b5172c8ec4f',
            )
    );

    public function getInfoByNameWithAccount($name, $account)
    {
        $info = M('weixin_token');
        $record = $info->where(array(
            'name' => $name,
            'account' => $account
        ))
            ->order('id desc')
            ->find();
        return $record;
    }

    public function addData($account, $name, $value)
    {
        $weixin = array();
        $info = M('weixin_token');
        $weixin['account'] = $account;
        $weixin['name'] = $name;
        $weixin['value'] = $value;
        $weixin['create_time']=date('Y-m-d H:i:s');
        $res = $info->add($weixin);
        return $res;
    }

    /**
     * 获取微信号的access token
     * 
     * @param string $weixin            
     */
    public function getAccessToken($weixin)
    {
        $info = $this->getInfoByNameWithAccount('access_token', $weixin);
        if (is_null($info) || strtotime($info['create_time']) < (time() - 6600)) {
            // 如果没有或记录超过了110分钟，则重新获取
            $accessToken = $this->refreshAccessToken($weixin);
        } else {
            $accessToken = $info['value'];
        }
        return $accessToken;
    }
    
    /**
     * 刷新access token
     * @param string $weixin 微信帐号
     * @return string 返回 access token
     */
    protected function refreshAccessToken($weixin)
    {
        $accontInfo = $this->accountsConf[$weixin];
    
        //获取token
        $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s';
        $url = sprintf($url, $accontInfo['appid'], $accontInfo['appsecret']);
        
        $resArr = json_decode($this->_html($url), true);
        if ($resArr['access_token']){
            $this->addData($weixin, 'access_token', $resArr['access_token']);
        }
    
        return $resArr['access_token'];
    }
    private function _html($url, $post = FALSE)
    {
        $curl = curl_init(); // 启动一个CURL会话
        curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 1); // 从证书中检查SSL加密算法是否存在
        curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器
        //curl_setopt($curl, CURLOPT_COOKIE, $cookie);
        curl_setopt($curl, CURLOPT_REFERER,'https://mp.weixin.qq.com');// 设置Referer
        curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求
        curl_setopt($curl, CURLOPT_POSTFIELDS, $post); // Post提交的数据包
        curl_setopt($curl, CURLOPT_TIMEOUT, 300); // 设置超时限制防止死循环
        curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
        $_str = curl_exec($curl); // 执行操作
        if (curl_errno($curl)) {
            echo 'Errno'.curl_error($curl);//捕抓异常
        }
        curl_close($curl); // 关闭CURL会话
        return $_str;
    }
    public function getPublicQrcode($qrcodeID,$weixin){
        
        $tempJson = '{ "action_name": "QR_LIMIT_SCENE", "action_info": {"scene": {"scene_id": '.$qrcodeID.'}}}';
         
        $access_token= $this->getAccessToken($weixin);
         
        $url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=" . $access_token;
        $tempArr = json_decode ( $this->_html( $url, $tempJson ), true );
        return $tempArr;
    }
}